<?php
/**
 * Created by PhpStorm.
 * User: pangaofeng
 * Date: 16/3/5
 * Time: 上午8:29
 */

//namespace Vendor\Alipay;


require_once 'lib/alipay_core.function.php';
require_once 'lib/alipay_md5.function.php';
require_once 'lib/alipay_rsa.function.php';
require_once 'lib/alipay_submit.class.php';
require_once 'lib/alipay_notify.class.php';

/**
 * Class Alipay
 * @package Vendor\Alipay
 */
class Alipay
{
    private $Config = array(
        'server'       =>  'create_direct_pay_by_user',    //接口名称默认为即时到账
        'partner'       =>  '',                             //合作身份ID
        'seller_email'  =>  '',                             //支付宝账户
        'key'           =>  '',                             //安全效验码
        'sign_type'     =>  'MD5',                          // RSA也可以
        //'input_charset'=>  'utf-8',                        //编码配置,不可修改
        '_input_charset'=>  'utf-8',                        //编码配置,不可修改
        'transport'     => 'http',                          //如果服务器支持SSL,请选用https
    );

    /**
     * Alipay constructor.
     * @param string $seller_email 支付宝账户
     * @param string $partner 合作身份ID
     * @param string $key 安全效验码
     * @param string $transport
     */
    public function __construct(){
//        $this->Config['seller_email'] = Config::alipay('seller_email');
//        $this->Config['partner']      = Config::alipay('partner');
//        $this->Config['key']          = Config::alipay('key');
//        $this->Config['ali_public_key'] = Config::alipay('ali_public_key');
//        $this->Config['private_key'] = Config::alipay('private_key');

//        $this->Config['seller_email'] = ' jzbwlkjtest@163.com';
//        $this->Config['partner']      = '2088421701715110';
//        $this->Config['key'] = 'nru9wktdvg2obedfzlgpa2ws2ucddnbf';

        $this->Config['seller_email'] = '17685344987';
        $this->Config['partner']      = '2088622878508835';
        $this->Config['key'] = 'zav8irxs41wfk4x2jfkwxw7lbyyw9o70';


        $this->Config['ali_public_key'] = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtSsIWn8B7xXYeiGPa4vfH0eRGqamqFAt2/eHa3hdGCA/dKMSctN/aYBCrFdbIsE+1aIgW/2H4wgWyX533jGihNuwW5jPs9yfF6JXjq4rdufojnC2horBTqEXxc/qz2biM5QjTtk3FiXqgu1dPt01NAX7S+aLLMftu7wp+Z4r94NXoIRQUw6liFDvVMfbHL5irTP4DXtuVxZdZvKXRTfYlAuvKGo4J+oQXe7WOfp+nYTR/xIj3PQ+teVgzb0+eWgCUPy6lguTo8D60NRBvYH6SnAuPjXl+EESfd/AaArxa9itgQWMQyD3FbaUKPoqaqQEXEqpGuL0Hg7y4m1d/qtYfQIDAQAB';

     //  $this->Config['ali_public_key'] = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB';
        
        $this->Config['private_key'] = 'MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC1KwhafwHvFdh6IY9ri98fR5EapqaoUC3b94dreF0YID90oxJy039pgEKsV1siwT7VoiBb/YfjCBbJfnfeMaKE27BbmM+z3J8XoleOrit25+iOcLaGisFOoRfFz+rPZuIzlCNO2TcWJeqC7V0+3TU0BftL5ossx+27vCn5niv3g1eghFBTDqWIUO9Ux9scvmKtM/gNe25XFl1m8pdFN9iUC68oajgn6hBd7tY5+n6dhNH/EiPc9D615WDNvT55aAJQ/LqWC5OjwPrQ1EG9gfpKcC4+NeX4QRJ938BoCvFr2K2BBYxDIPcVtpQo+ipqpARcSqka4vQeDvLibV3+q1h9AgMBAAECggEAXs8zYvCemXlMPbX8cWLUlHEGjfTyuJilNz1V4DcwOOT/cFG64N6wCg02VyysJAcPlAtxddK7X2OnUTFfF6fQDklOBNF2uQDXPS4XNjMNyf9ZY9m2m7hXq4eAgmPp1TpZXHbE94mTW4LiNDx2FOT858nNlg4DfZ7vWdUB/oD5L2DpE6XwDb/Thk8hbxZpoQHcfhvl7/saYtXNBMn2NFNkN5rHqtv2WUy3XiSagHXfVfoG7v/J4HTjSKdKSqjTDO9QNGP+uRKZ1VoUWTvwbaZVREhYrXpl7lklFDfhHc8zcpbDs1GL8Th7mgZjFTpE/pLablXM4zw7wJGa4QMrAlpPQQKBgQDw3daIE6Z/AYcGnKm6b1X235B1GYKK80mmU4v/09JOrB3EWETpM/CqoLSW01Jt+oeok8m7wJjM11Sj3BkZhAmvOtu6a7UywKiVhGyWNK+pCVWdrPRrAno//sb/qZ7duBxdMOeWD3qcvGOx4QihvKtCzBqTEtbFejDszZX1glzGzQKBgQDAjP0ZYOZO+0UwmvqCs6MIqAngF6LLFjruyMQpt4iKsnxTKliUGXlST5YCAeTzuppb1QfGZ4BMK8z9VJ+KhpjMUoU08b3Jz9SwqYikWXhrSO3covAcIvq10uATHRxbdu45iBTzUoLavpVUWhnttXWeDA/n/AWXZPD6wOvFNaX4cQKBgQDUqomoS/FNYgPeCqSxP2wfNPVO7t8b4AK/IDUUA+pEHCOMIIN3g6ySeqH4KU02XWU5qoMLitlewyCwLLzckoAaf1VSG5c44HH40ZsuZAZUsTEurm5/rQhTSJhbU2A5Uz136p4PGEuh55P+kjoU04hCygMMqC2I3oIkEsC8rAVOYQKBgQCoP0m00D/Nya82fmTgqfiOSs5+OgjsbdtG5LTLQuyTTMxnmvvVRMEG1DXeQSt+cJIKJgXqLmwjoq42uafCUkPXXU+56HWOjwRo7WpROUCIdiwpe1JVM5dgIwfai7hIj44vRSIRTwq5+offMe+wDrvcQuvvJ/72v6P1Z3dcVblVcQKBgQDB97Wc7fjP4KC7r942sXZh/6gfpi1O9UubKXBZNtkGBCe6Bnl43nzwSHm7xjXpO05cVcv+alq7R9RH/o4+0jLqCohRVw91REUxxJIeDPWcTev7qv1s6NoCri6wqJSQZdq+jVTXFrJdX7yLs8I05Q5Q2Zfmd37KrJ/q6AFtxbHz6A==';


    }

    /**
     * @param string $subject 商品名 不知为何如果是中文就乱码
     * @param string $total_fee 价格   要支付的价格
     * @param string $out_trade_no 订单号
     * @param string $notify_url 通知网址
     * @param string $return_url 回调网址
     * @param string $btn_value
     * @return 提交表单HTML文本
     */
    public function buildPayForm($subject,$total_fee,$out_trade_no,$notify_url,$return_url,$btn_value='确认支付'){
        $parameter = array(
            'service'       =>  $this->Config['server'],
            'partner'       =>  trim($this->Config['partner']),
            'seller_email'  => $this->Config['seller_email'],
            'payment_type'  => 1,
            'out_trade_no'  => $out_trade_no,
            'notify_url'    => $notify_url,
            'subject'       => $subject,
            'total_fee'     => $total_fee,
            'return_url'    => $return_url,
            '_input_charset' => $this->Config['_input_charset'],
        );
        $alipaySubmit = new AlipaySubmit($this->Config);
        return $alipaySubmit->buildRequestForm($parameter,"get", $btn_value);
    }

    /**
     * 返回验证 返回[订单号=>trade_no, 金额=>total_fee]或者False
     */
    public function verifyReturn(){
        $alipayNotify = new AlipayNotify($this->Config);
        return $alipayNotify->verifyReturn();
    }
    /**
     * 通知验证 返回[订单号=>trade_no, 金额=>total_fee]或者False
     */
    public function verifyNotify(){
        $alipayNotify = new AlipayNotify($this->Config);
        return $alipayNotify->verifyNotify();
    }
}